package com.zengqingfa.designpattern.create.singleton;

/**
 * @fileName: Singleton2
 * @author: zengqf3
 * @date: 2021-4-5 21:00
 * @description: 静态代码块
 * 将类实例化的过程放在了静态代码块中，在类装载的时执行静态代码块中的代码，初始化类的实例
 */
public class Singleton2 {

    /**
     * 构造函数私有
     */
    private Singleton2() {
    }

    public static Singleton2 instance;

    /**
     * 静态代码块初始化
     */
    static {
        instance = new Singleton2();
    }

    //获取实例

    public static Singleton2 getInstance() {
        return instance;
    }

    public static void main(String[] args) {
        Singleton2 instance1 = Singleton2.getInstance();
        Singleton2 instance2 = Singleton2.getInstance();
        //com.zengqingfa.designpattern.create.singleton.Singleton2@58372a00
        System.out.println(instance1);
        //ture
        System.out.println(instance2 == instance1);
    }
}
